package edu.pku.sei.transformation.editor.model.deploy;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.ListProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.common.MetaTypedList;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.Relationship;
import edu.pku.sei.transformation.editor.model.TypedModel;

public class Deployment extends Relationship {
	public static final int MODE_CHECKONLY = 0;
	public static final int MODE_ENFORCE = 1;
	private TypedModel direction = null;
	private int executionMode = 0; 
	private MetaTypedList actualParameters = new MetaTypedList(ActualParameter.class, this, ModelPackage.__ACTUAL_PARAMETER__);
	
	@MethodProperty(catalog = "deploy", id = ModelPackage.TRANSEXE_DIRECTION, setter = "setDirection", name = "direction")
	@EnumerationProperty(function = "getDirectionCandidates")
	public TypedModel getDirection() {
		return this.direction;
	}

	public void setDirection(TypedModel outputPara) {
		this.direction = outputPara;
		this.firePropertyChange("direction", null, null);
	}

	public Object[] getDirectionCandidates() {
		if (this.getCode().getSpecification() != null)
			return this.getCode().getSpecification().getTypedModel().toArray();
		else
			return new Object[0];
	}
	
	public TransformationSourceCode getCode() {
		return (TransformationSourceCode) sourceElement;
	}
	
	public void setCode(TransformationSourceCode code) {
		this.sourceElement = code;
		
	}
	
	public TransformationEngine getEngine() {
		return (TransformationEngine) targetElement;
	}
	
	public void setEngine(TransformationEngine engine) {
		this.targetElement = engine;
	}
	
	@MethodProperty(id = ModelPackage.TRANSEXE_MODE, catalog = "deploy", name = "mode", setter = "setModeString")
	@EnumerationProperty(function = "getModeStrings")
	public String getModeString() {
		if (executionMode == MODE_CHECKONLY)
			return "checkOnly";
		else if (executionMode == MODE_ENFORCE)
			return "enforce";
		return "checkOnly";
	}

	public int getExecutionMode() {
		return executionMode;
	}

	public void setExecutionMode(int executionMode) {
		this.executionMode = executionMode;
		this.firePropertyChange("mode", null, null);
	}
	public void setModeString(String modeString) {
		if ("checkOnly".equals(modeString)) {
			setExecutionMode(MODE_CHECKONLY);
		} else if ("enforce".equals(modeString)) {
			setExecutionMode(MODE_ENFORCE);
		} else {
			setExecutionMode(MODE_CHECKONLY);
		}
	}

	public String[] getModeStrings() {
		return new String[] { "checkOnly", "enforce" };
	}
	
	@MethodProperty(id = ModelPackage.__ACTUAL_PARAMETER__, catalog = "deploy", name="actual parameters")
	@ListProperty(allowTypes = ActualParameter.class, newInstanceMethod = "newInstance",typeString = ModelPackage.__ACTUAL_PARAMETER__)
	public MetaTypedList getActualParameters() {
		return actualParameters;
	}
}
